-----------------------------------------------------------------------------------------
--
-- tutorial.lua
--
-----------------------------------------------------------------------------------------

local storyboard = require( "storyboard" )
local scene = storyboard.newScene()

-- include Corona's "widget" library
local widget = require "widget"
local audioGame = require "audioGame";
--------------------------------------------
local arrowLeft
local arrowRight
local touchLeft
local touchRight
local background
local titleLogo
local backBtn
local i = 0
-- forward declarations and other locals

local listenerBackBtn = function( obj )
            transition.to( backBtn, { time = 200, alpha=1.0,x = display.contentWidth*0.5, y=5*display.contentHeight/6} )
        end

-- 'onRelease' event listener for playBtn
local function onBackBtnRelease( )
	backBtn.xScale = 1;
        backBtn.yScale = 1;
        storyboard.removeScene( "tutorial" );
        local menu = storyboard.loadScene( "menu" )
        -- Later, transition to the scene (no loading necessary)
        storyboard.gotoScene( "menu", "fade", 100 )
	return true	-- indicates successful touch
    end
-- "OnPress" event listener for backBtn
local function onBackBtnPress()
    audio.play(audioGame.buttonClick);
    backBtn.xScale = 1.5;backBtn.yScale = 1.5;
    return true;
end 


-- onRelease event listener for onlineBtn 

-- Move button 

local listener2 = function (obj) 
   end
-----------------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
-- 
-- NOTE: Code outside of listener functions (below) will only be executed once,
--		 unless storyboard.removeScene() is called.
-- 
-----------------------------------------------------------------------------------------

-- Called when the scene's view does not exist:
function scene:createScene( event )
        print("Tao sceen Tutorial")
	local group = self.view
        --display background image
        local background = display.newImageRect( "images/menu/backgroundCity.png", display.contentWidth, display.contentHeight )
	background:setReferencePoint( display.TopLeftReferencePoint )
	background.x, background.y = 0, 0
        
        -- create title logonew
        local titleLogo = display.newImage( "images/tutorial/lr_logo_tutorial.png")
        titleLogo:setReferencePoint( display.CenterReferencePoint )
        titleLogo.x = display.contentWidth/2
	titleLogo.y = 40

	-- create arrow left images
	arrowLeft = display.newImage( "images/tutorial/lr_tut_arrowleft.png", display.contentWidth, display.contentHeight )
	arrowLeft:setReferencePoint( display.TopLeftReferencePoint )
	arrowLeft.x = display.contentWidth/4
	arrowLeft.y = 140
	
	-- create arrow right images
	arrowRight = display.newImage( "images/tutorial/lr_tut_arrowright.png" )
	arrowRight:setReferencePoint( display.CenterReferencePoint )
        arrowRight.x = 3*display.contentWidth/4
	arrowRight.y = 200
        
        -- create touchleft images
	touchLeft = display.newImage( "images/tutorial/lr_tut_touchleft.png" )
	touchLeft:setReferencePoint( display.CenterReferencePoint )
        touchLeft.x = display.contentWidth/4
	touchLeft.y = 120
        
        -- create touchright images
	touchRight = display.newImage( "images/tutorial/lr_tut_touchright.png" )
	touchRight:setReferencePoint( display.CenterReferencePoint )
        touchRight.x = 3*display.contentWidth/4
	touchRight.y = 100
        
        -- print("EnterScene");
	
        group:insert( background )
        group:insert( titleLogo )
        group:insert( arrowLeft )
        group:insert( arrowRight )
        group:insert( touchLeft )
	group:insert( touchRight )
      
end

-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
       local group = self.view;
       backBtn  = widget.newButton{
		labelColor = { default={255}, over={128} },
		default = "images/menu/lr_menu_back.png",
		width = 150, height = 30,
                onPress = onBackBtnPress,
		onRelease = onBackBtnRelease -- event listener function
	}
	backBtn:setReferencePoint( display.CenterReferencePoint )
	backBtn.x = display.contentWidth*0.5
	backBtn.y = 5*display.contentHeight/6+30
        
	-- all display objects must be inserted into group
	transition.from( backBtn, { time = 200,  x=display.contentWidth/2, y=0, onComplete=listenerBackBtn } )
        group:insert( backBtn )
        
end

-- Called when scene is about to move offscreen:
function scene:exitScene( event )
	local group = self.view
        if backBtn then 
           backBtn:removeSelf()
           backBtn = nil
          -- print ("remove backBtn")
        end
	-- INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)
	
end

-- If scene's view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
	local group = self.view
        --backBtn:removeSelf()	-- widgets must be manually removed
        --storyboard:removeSelf("menu")
        --backBtn = nil
        print("Xoa backBtn");                                                         
end

-----------------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
-----------------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched whenever before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )

-----------------------------------------------------------------------------------------

return scene